﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVCMusicStore.Models;
namespace MVCMusicStore.Models
{
    public partial class Cart
    {
        public void Test()
        {
            
        }

        MusicStoreEntities db = new MusicStoreEntities();
        private string ShoppingCartId { get; set; }
        public const string CartSessionKey = "CardId";
        /// <summary>
        /// Add album into Cart, if album exist then increase the quantity, otherwise just add it as new item
        /// </summary>
        /// <param name="album"></param>
        public void AddAlbumToCart(Album album)
        { 
            var cartitem = db.Carts.SingleOrDefault(c => c.CartId == ShoppingCartId && c.AlbumId == album.AlbumId);

            if (cartitem == null)
            {
                cartitem = new Cart()
                {
                    CartId = ShoppingCartId,
                    AlbumId = album.AlbumId,
                    Count = 1,
                    DateCreated = DateTime.Now
                };
                db.AddToCarts(cartitem);
            }
            else
            {
                //to^?ng co^.ng co' count mo'n ha`ng trong gio?
                cartitem.Count++;
            }
            db.SaveChanges();
        }
        public void RemoveAlbumFromCart(Album album)
        {
            var cartItem = db.Carts.SingleOrDefault(c => c.CartId == ShoppingCartId && c.AlbumId == album.AlbumId);
            if (cartItem != null)
            {
                if (cartItem.Count > 1)
                    cartItem.Count--;
                else
                    db.Carts.DeleteObject(cartItem);
                db.SaveChanges();
            }
        }
        public void EmptyCart()
        {
            var carts = db.Carts.Where(c => c.CartId == ShoppingCartId);
            foreach (Cart cartItem in carts)
            {
                db.Carts.DeleteObject(cartItem);
            }
        }
        public List<Cart> GetCartItems()
        {
            var carts = (from c in db.Carts
                        where c.CartId == ShoppingCartId
                         select c).ToList(); ;
            return carts;
        }
        public int GetCount()
        {
            var albums = from c in db.Carts
                         where c.CartId == ShoppingCartId
                         select c;
            return albums.ToList().Count;
        }
        public decimal GetTotal()
        {
            decimal? total = (from c in db.Carts
                         where c.CartId == ShoppingCartId
                         select (int?)c.Count*c.Album.Price).Sum();
            return total ?? decimal.Zero;
        }
        public int CreateOrder(Order order)
        {
            decimal orderTotal = 0;
            var carts = GetCartItems();
            foreach (var item in carts)
            {
                OrderDetail detail = new OrderDetail()
                {
                    OrderId=order.OrderId,
                    AlbumId = item.AlbumId,
                    Quantity = item.Count,
                    UnitPrice = item.Album.Price
                };
                db.OrderDetails.AddObject(detail);
                orderTotal += item.Count * item.Album.Price;
            }
            db.SaveChanges();
            EmptyCart();
            return order.OrderId;
        }
        public string GetCardId(HttpContextBase context)
        {
            if (context.Session[CartSessionKey] == null)
            {
                if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CartSessionKey] = context.User.Identity.Name;
                }
                else
                {
                    System.Guid tempCardId = Guid.NewGuid();
                    context.Session[CartSessionKey] = tempCardId.ToString();
                }
            }
            return context.Session[CartSessionKey].ToString();
        }
        public void MirgrateCart(string username)
        {
            var shoppingCart = from c in db.Carts
                               where c.CartId == ShoppingCartId
                               select c;
            foreach (Cart c in shoppingCart)
            {
                c.CartId = username;
            }
            db.SaveChanges();

        }
    }
}